package com.ktg.mes.wm.controller;

import java.math.BigDecimal;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ktg.common.utils.StringUtils;
import com.ktg.mes.md.domain.MdItem;
import com.ktg.mes.md.service.IMdItemService;
import com.ktg.mes.wm.domain.*;
import com.ktg.mes.wm.service.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ktg.common.annotation.Log;
import com.ktg.common.core.controller.BaseController;
import com.ktg.common.core.domain.AjaxResult;
import com.ktg.common.enums.BusinessType;
import com.ktg.common.utils.poi.ExcelUtil;
import com.ktg.common.core.page.TableDataInfo;

/**
 * 生产退料单行Controller
 *
 * @author yinjinlu
 * @date 2022-09-15
 */
@RestController
@RequestMapping("/mes/wm/rtissueline")
public class WmRtIssueLineController extends BaseController
{
    @Autowired
    private IWmRtIssueLineService wmRtIssueLineService;

    @Autowired
    private IWmWarehouseService wmWarehouseService;

    @Autowired
    private IWmStorageLocationService wmStorageLocationService;

    @Autowired
    private IWmStorageAreaService wmStorageAreaService;

    @Autowired
    private IWmItemRecptLineService wmItemRecptLineService;

    @Autowired
    private IMdItemService mdItemService;

    @Autowired
    private IWmMaterialStockService wmMaterialStockService;

    /**
     * 查询生产退料单行列表
     */
    @PreAuthorize("@ss.hasPermi('mes:wm:rtissue:list')")
    @GetMapping("/list")
    public TableDataInfo list(WmRtIssueLine wmRtIssueLine)
    {
        startPage();
        List<WmRtIssueLine> list = wmRtIssueLineService.selectWmRtIssueLineList(wmRtIssueLine);
        return getDataTable(list);
    }

    /**
     * 导出生产退料单行列表
     */
    @PreAuthorize("@ss.hasPermi('mes:wm:rtissue:export')")
    @Log(title = "生产退料单行", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WmRtIssueLine wmRtIssueLine)
    {
        List<WmRtIssueLine> list = wmRtIssueLineService.selectWmRtIssueLineList(wmRtIssueLine);
        ExcelUtil<WmRtIssueLine> util = new ExcelUtil<WmRtIssueLine>(WmRtIssueLine.class);
        util.exportExcel(response, list, "生产退料单行数据");
    }

    /**
     * 获取生产退料单行详细信息
     */
    @PreAuthorize("@ss.hasPermi('mes:wm:rtissue:query')")
    @GetMapping(value = "/{lineId}")
    public AjaxResult getInfo(@PathVariable("lineId") Long lineId)
    {
        return AjaxResult.success(wmRtIssueLineService.selectWmRtIssueLineByLineId(lineId));
    }

    /**
     * 新增生产退料单行
     */
    @PreAuthorize("@ss.hasPermi('mes:wm:rtissue:add')")
    @Log(title = "生产退料单行", businessType = BusinessType.INSERT)
    @PostMapping
    @Transactional
    public AjaxResult add(@RequestBody WmRtIssueLine wmRtIssueLine)
    {
        WmWarehouse warehouse = null;
        if(StringUtils.isNotNull(wmRtIssueLine.getWarehouseId())){
            warehouse = wmWarehouseService.selectWmWarehouseByWarehouseId(wmRtIssueLine.getWarehouseId());
            wmRtIssueLine.setWarehouseCode(warehouse.getWarehouseCode());
            wmRtIssueLine.setWarehouseName(warehouse.getWarehouseName());
        }
        if (warehouse == null){
            return error("仓库不能为空");
        }
        if(StringUtils.isNotNull(wmRtIssueLine.getLocationId())){
            WmStorageLocation location = wmStorageLocationService.selectWmStorageLocationByLocationId(wmRtIssueLine.getLocationId());
            wmRtIssueLine.setLocationCode(location.getLocationCode());
            wmRtIssueLine.setLocationName(location.getLocationName());
        }
        if(StringUtils.isNotNull(wmRtIssueLine.getAreaId())){
            WmStorageArea area = wmStorageAreaService.selectWmStorageAreaByAreaId(wmRtIssueLine.getAreaId());
            wmRtIssueLine.setAreaCode(area.getAreaCode());
            wmRtIssueLine.setAreaName(area.getAreaName());
        }
        int a = wmRtIssueLineService.insertWmRtIssueLine(wmRtIssueLine);

        //退料创建入库单
        WmItemRecptLine itemRecptLine = wmItemRecptLineService.createRecptLineFromWmRtIssueLine(wmRtIssueLine);


        //仓库增加

        return toAjax(a);
    }

    /**
     * 修改生产退料单行
     */
    @PreAuthorize("@ss.hasPermi('mes:wm:rtissue:edit')")
    @Log(title = "生产退料单行", businessType = BusinessType.UPDATE)
    @PutMapping
    @Transactional
    public AjaxResult edit(@RequestBody WmRtIssueLine wmRtIssueLine)
    {
        WmWarehouse warehouse = null;
        if(StringUtils.isNotNull(wmRtIssueLine.getWarehouseId())){
            warehouse = wmWarehouseService.selectWmWarehouseByWarehouseId(wmRtIssueLine.getWarehouseId());
            wmRtIssueLine.setWarehouseCode(warehouse.getWarehouseCode());
            wmRtIssueLine.setWarehouseName(warehouse.getWarehouseName());
        }
        if (warehouse == null){
            return error("仓库不能为空");
        }
        if(StringUtils.isNotNull(wmRtIssueLine.getLocationId())){
            WmStorageLocation location = wmStorageLocationService.selectWmStorageLocationByLocationId(wmRtIssueLine.getLocationId());
            if (location != null){
                wmRtIssueLine.setLocationCode(location.getLocationCode());
                wmRtIssueLine.setLocationName(location.getLocationName());
            }
        }
        if(StringUtils.isNotNull(wmRtIssueLine.getAreaId())){
            WmStorageArea area = wmStorageAreaService.selectWmStorageAreaByAreaId(wmRtIssueLine.getAreaId());
            if (area != null){
                wmRtIssueLine.setAreaCode(area.getAreaCode());
                wmRtIssueLine.setAreaName(area.getAreaName());
            }
        }
        WmItemRecptLine sss = new WmItemRecptLine();
        sss.setAttr1(String.valueOf(wmRtIssueLine.getLineId()));
        QueryWrapper<WmItemRecptLine> itemRecptLineQueryWrapper = new QueryWrapper<>(sss);

        wmRtIssueLineService.updateWmRtIssueLine(wmRtIssueLine);

        WmItemRecptLine itemRecptLine = wmItemRecptLineService.getOne(itemRecptLineQueryWrapper);
        if (itemRecptLine != null){
            //修改总库存量
            MdItem mdItem = mdItemService.getById(itemRecptLine.getItemId());
            WmItemRecptLine oldItemRecptLine =  itemRecptLine;

            itemRecptLine.setQuantityRecived(wmRtIssueLine.getQuantityRt());
            itemRecptLine.setWarehouseId(wmRtIssueLine.getWarehouseId());
            itemRecptLine.setWarehouseCode(wmRtIssueLine.getWarehouseCode());
            itemRecptLine.setWarehouseName(wmRtIssueLine.getWarehouseName());
            itemRecptLine.setLetStepMaterial(wmRtIssueLine.getLetStepMaterial());
            wmItemRecptLineService.updateById(itemRecptLine);
        }

        return toAjax(1);
    }

    /**
     * 删除生产退料单行
     */
    @PreAuthorize("@ss.hasPermi('mes:wm:rtissue:remove')")
    @Log(title = "生产退料单行", businessType = BusinessType.DELETE)
	@DeleteMapping("/{lineIds}")
    @Transactional
    public AjaxResult remove(@PathVariable Long[] lineIds)
    {
        for (int i = 0; i < lineIds.length; i++) {
            WmItemRecptLine wmItemRecptLine = new WmItemRecptLine();
            wmItemRecptLine.setAttr1(String.valueOf(lineIds[0]));
            QueryWrapper<WmItemRecptLine> queryWrapper = new QueryWrapper<>(wmItemRecptLine);
            wmItemRecptLine = wmItemRecptLineService.getOne(queryWrapper);
            MdItem mdItem = mdItemService.getById(wmItemRecptLine.getItemId());
            WmWarehouse oldWareHouse = wmWarehouseService.selectWmWarehouseByWarehouseId(wmItemRecptLine.getWarehouseId());

            wmItemRecptLineService.deleteWmItemRecptLineByLineId(wmItemRecptLine.getLineId());
        }
        return toAjax(wmRtIssueLineService.deleteWmRtIssueLineByLineIds(lineIds));
    }
}
